package com.yeyv.net.Web.Control.Anonymous;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yeyv.net.Annotation.ControllerApi.AnonController;
import com.yeyv.net.UsefulEntity.Resp;
import com.yeyv.net.Web.Entity.OpensourceCalendarEntity;
import com.yeyv.net.Web.Service.OCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * OpenCalendar Controller
 *
 * @author 若雨reYour
 **/
@AnonController("/oc")
public class OCController {
    @Autowired
    private OCService ocService;

    @GetMapping("/list")
    public Resp<List<OpensourceCalendarEntity>> listOCList(@RequestParam(defaultValue = "0") int baseId) {
        if (baseId == 0) {
            return Resp.success(ocService.getBaseOC());
        }
        return null;
    }

    @GetMapping("/listDirVersion")
    public Resp<List<OpensourceCalendarEntity>> listOCList(@RequestParam String dirName) {
        return Resp.success(ocService.getDirVersions(dirName));
    }

    @GetMapping("/listDir")
    public Resp<List<OpensourceCalendarEntity>> listDir(@RequestParam(defaultValue = "0") int id) {
        return Resp.success(ocService.getDirList(id));
    }

    @GetMapping("/getFileParents")
    public Resp<List<OpensourceCalendarEntity>> getFileParents(@RequestParam(defaultValue = "0") int id) {
        List<OpensourceCalendarEntity> list = new ArrayList<>();
        int baseId = id;
        while (baseId != -1 && baseId != 0) {
            OpensourceCalendarEntity entity = ocService.getById(baseId);
            baseId = entity.getBaseId();
            list.add(entity);
        }
        return Resp.success(list);
    }
}
